<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>药品保质期检查</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/static/admin/style.css}" media="all">
    <script type="text/html" id="indexTpl">
        {{d.LAY_TABLE_INDEX+1}}
    </script>
</head>
<body class="layui-layout-body">
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">药品保质期检查</div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="main-table" lay-filter="main-table"></table>
                    <script type="text/html" id="toolbarDemo">
                        <span class="layui-inline" lay-event="add"><i class="layui-icon">&#xe654;</i></span>
                        <span class="layui-inline" style="width: 240px;">
                                   <input class="layui-input" name="param" id="param" placeholder="根据药品名称查询" autocomplete="off">
                               </span>
                        <span class="layui-btn layui-btn-sm layui-bg-red" lay-event="querySearch">
                                   <i class="layui-icon">&#xe615;</i>查询
                               </span>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<script th:src="@{/static/layui/layui.js}"></script>
<script>
    layui.use(['table','form'],function () {
        var $ = layui.$;
        var table = layui.table;
        table.render({
            elem:'#main-table',
            height:650,
            url:'/druginfo/druginfoQueryPage',
            method:'POST',
            page:true,
            id:'table-load',
            toolbar:'#toolbarDemo',
            cols:[
                [
                    {title:'序号',templet:'#indexTpl',width:60,align:'center'},
                    {field:'name',title:'药品名称',align:'center'},
                    {field:'warrenty',title:'保质期(月)',align:'center'},
                    {field:'producttime',title:'生产时间',align:'center',
                        templet: "<div>{{layui.util.toDateString(d.producttime,'yyyy-MM-dd')}}</div>"},
                    {field:'createtime',title:'当前时间',align:'center',
                        templet: "<div>{{layui.util.toDateString(d.createtime,'yyyy-MM-dd')}}</div>"},
                    {title:'是否过期',align:'center',templet: iswarranty}

                ]
            ]
        });
        //监听头部工具栏事件
        table.on('toolbar(main-table)',function (obj) {
            switch (obj.event) {
                case 'querySearch':
                    var param = $('#param').val();
                    table.reload('table-load',{
                        where:{
                            param:param
                        }
                    });
                    $('#param').valueOf(param);
                    break;
            }
        })
        //计算药品是否过期
        function iswarranty(d) {
            debugger
            //生产日期
            var p_time = d.producttime;
            p_time = p_time.split('-');
            p_time = parseInt(p_time[0])*12 + parseInt(p_time[1]);
            //获取当前时间
            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth()+1;
            var now_year_month = year*12 + month;
            var m = now_year_month - (p_time +parseInt(d.warrenty));
            if (m >= 1){
                return "<span style='color:red'>已过期"+m+"个月</span>";
            }else if (m == 0){
                return "<span style='color:blue'>保质期不满一个月</span>";
            }else{
                return "<span style='color:green'>未过期，剩余"+Math.abs(m)+"个月</span>";
            }

        }
    });
</script>
</body>
</html>